package com.eric.rabbitmq.work.fair;

import com.eric.rabbitmq.utils.RabbitMqUtils;
import com.eric.rabbitmq.utils.SleepUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

public class Work04 {
    //队列名称
    public static  final  String task_queue_name = "ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        System.out.println("C4等待接收消息处理时间较长");
        //声明，接收消息
        DeliverCallback deliverCallback = (consumerTag, message)->{
            //睡30S
            SleepUtils.sleep(30);
            System.out.println("接收到的消息:"+ new String(message.getBody(),"UTF-8"));
            //手动应答
            channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
        };
        //设置不公平分发，实现多劳多得
        int prefetchCount = 5;
        channel.basicQos(prefetchCount);
        //采取手动应答
        boolean autoAck = false;
        channel.basicConsume(task_queue_name,autoAck,deliverCallback,(consumerTag ->{
            System.out.println(consumerTag + "消费者取消消费接口回调逻辑");
        }));
    }
}
